/*
   Chiroptera - A Java client for Bacula

   The main author of Chiroptera is Marcos Paulo Serafim.
   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version two of the GNU General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/
package br.eti.mps.chiroptera.lib.parsers;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Map;

import br.eti.mps.chiroptera.constants.Constants;
import br.eti.mps.chiroptera.lib.ChiropteraException;
import br.eti.mps.chiroptera.lib.Job;
import br.eti.mps.chiroptera.lib.StatusDirector;

/**
 *
 * @author <a href="mailto:mpserafim[at]gmail[dot]com">Marcos Paulo Serafim</a>
 */
//TODO: Convert to JavaCC Grammar
public class ParseStatusDirector {

	public static StatusDirector parse(Map<String, Object> configuration,
			String input) throws ChiropteraException {

		String language = (String)configuration.get(Constants.Chiroptera.ConfigurationKeys.SERVER_LANGUAGE);
		String country = (String)configuration.get(Constants.Chiroptera.ConfigurationKeys.SERVER_COUNTRY);
		
		Locale locale = new Locale(language, country);
		
		DateFormat df = new SimpleDateFormat(Constants.Bacula.DATE_FORMAT, locale);
		
		StatusDirector sd = new StatusDirector();

		StringBuffer sbTemp = new StringBuffer();
		String temp;
		
		Parser p = new Parser(input);
		
		//Banner
		sbTemp.append(p.getToken(Constants.CR)).append(Constants.CR);
		sbTemp.append(p.getToken(Constants.CR)).append(Constants.CR);
		sbTemp.append(p.getToken(Constants.CR));
		sd.setBanner(sbTemp.toString());
		
		p.getToken(Constants.CR); //Blank Line
		
		temp = p.getToken(Constants.CR).trim();
		//Scheduled jobs
		if (temp.equalsIgnoreCase(Constants.Connection.Tokens.SCHEDULED_JOBS)) {
			p.getToken(Constants.CR); //Titles
			p.getToken(Constants.CR); //Line
			
			temp = p.getToken(Constants.CR).trim();			
			
			if (!temp.equalsIgnoreCase(Constants.Connection.Tokens.NO_SCHEDULED_JOBS)) {
				
				while (!temp.equalsIgnoreCase(Constants.Connection.Tokens.END_JOBS)) {
					sd.getScheduledJobs().add(parseScheduledJob(temp, df));
					temp = p.getToken(Constants.CR).trim();
				}
				
			}
			
		}
		
		p.getToken(Constants.CR); //Blank Line
		
		//Running jobs
		temp = p.getToken(Constants.CR).trim();
		if (temp.equalsIgnoreCase(Constants.Connection.Tokens.RUNNING_JOBS)) {
			p.getToken(Constants.CR); //Titles
			p.getToken(Constants.CR); //Line
			
			temp = p.getToken(Constants.CR).trim();			
			
			if (!temp.equalsIgnoreCase(Constants.Connection.Tokens.NO_RUNNING_JOBS)) {
				
				while (!temp.equalsIgnoreCase(Constants.Connection.Tokens.END_JOBS)) {
					sd.getScheduledJobs().add(parseRunningJob(temp));
					temp = p.getToken(Constants.CR).trim();
				}
				
			}
		}
		
		p.getToken(Constants.CR); //Blank Line
		
		//Terminated jobs
		temp = p.getToken(Constants.CR).trim();
		if (temp.equalsIgnoreCase(Constants.Connection.Tokens.TERMINATED_JOBS)) {
			p.getToken(Constants.CR); //Titles
			p.getToken(Constants.CR); //Line
			
			temp = p.getToken(Constants.CR).trim();			
			
			if (!temp.equalsIgnoreCase(Constants.Connection.Tokens.NO_TERMINATED_JOBS)) {
				
				while (!temp.equalsIgnoreCase(Constants.Connection.Tokens.END_JOBS) && temp.trim().length() > 0) {
					sd.getScheduledJobs().add(parseTerminatedJob(temp));
					temp = p.getToken(Constants.CR).trim();
				}
				
			}
		}
		
		
		
		return sd;
	}

	private static Job parseScheduledJob(String line, DateFormat df) throws ChiropteraException {
		Job job = new Job();
		
		job.setLevel(line.substring(0, 14).trim());
		job.setType(line.substring(15, 24).trim());
		job.setPriority(Integer.parseInt(line.substring(25, 27)));
		try {
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(df.parse(line.substring(48, 67).trim()));
			job.setScheduled(calendar);
		} catch (ParseException pe) {
			throw new ChiropteraException(Constants.Chiroptera.Errors.DATE_PARSE, pe.getMessage(), pe);
		}
		job.setName(line.substring(48, 67).trim());
		job.setVolume(line.substring(67).trim());
		
		return job;
	}
	
	private static Job parseRunningJob(String line) {
		Job job = new Job();
		return job;
	}
	
	private static Job parseTerminatedJob(String line) {
		Job job = new Job();
		return job;
	}

}
